package com.itcodai.utils.fastjson;

import com.alibaba.fastjson.*;
import com.itcodai.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

// https://alibaba.github.io/fastjson2/
// https://repo1.maven.org/maven2/com/alibaba/fastjson/

/**
 * @author c-fanqj
 * @date 2023/5/26
 * @apiNote
 */
public class FastJsonTests {

    private static final Logger logger = LoggerFactory.getLogger(FastJsonTests.class);

    public static void main(String[] args) {
        //System.out.println("Hello world!");
        //logger.info("Hello, world!");
        test();
    }

    public static void test() {
        // 2 简单使用
        // 2.1 将JSON解析为JSONObject
        String text = "{\"name\":\"jiejie\"}";
        JSONObject jsonObject = JSON.parseObject(text);
        //logger.info("jsonObject - {}", jsonObject);
        //  jsonObject - {"name":"jiejie"}

        byte[] bytes = {123, 34, 105, 100, 34, 58, 49, 48, 48, 44, 34, 112, 97, 115, 115, 119, 111, 114, 100, 34, 58, 34, 49, 50, 51, 52, 34, 44, 34, 117, 115, 101, 114, 110, 97, 109, 101, 34, 58, 34, 106, 105, 101, 106, 105, 101, 34, 125};
        //jsonObject = JSON.parseObject(bytes);
        //logger.info("jsonObject2 - {}", jsonObject);
        // jsonObject2 - {"password":"1234","id":100,"username":"jiejie"}

        // 2.2 将JSON解析为JSONArray
        text = "[{\"name\":\"jiejie\"},{\"name\":\"haha\"}]";
        JSONArray jsonArray;
        //jsonArray = jsonObject.parseArray(text);
        //logger.info("jsonArray - {}", jsonArray);
        // jsonArray - [{"name":"jiejie"},{"name":"haha"}]
        jsonArray = JSON.parseArray(text);
        //logger.info("jsonArray - {}", jsonArray);
        // jsonArray - [{"name":"jiejie"},{"name":"haha"}]

        // 2.3 将JSON解析为Java对象
        text = "{\"id\":100L,\"username\":\"jiejie\",\"password\":\"1234\"}";
        User user = JSON.parseObject(text, User.class);
        //logger.info("user - {}", user);
        // user - User(id=100, username=jiejie, password=1234)

        // 2.4 将Java对象序列化为JSON
        Object data = "";
        data = user;
        text = JSON.toJSONString(data);
        bytes = JSON.toJSONBytes(data);
        //logger.info("text - {}", text);
        //  text - {"id":100,"password":"1234","username":"jiejie"}
        //logger.info("bytes - {}", bytes);
        // bytes - [123, 34, 105, 100, 34, 58, 49, 48, 48, 44, 34, 112, 97, 115, 115, 119, 111, 114, 100, 34, 58, 34, 49, 50, 51, 52, 34, 44, 34, 117, 115, 101, 114, 110, 97, 109, 101, 34, 58, 34, 106, 105, 101, 106, 105, 101, 34, 125]

        // 2.5 使用 JSONObject、JSONArray
        // 2.5.1 获取简单属性
        text = "{\"id\": 2,\"name\": \"fastjson2\"}";
        jsonObject = JSON.parseObject(text);
        int id = jsonObject.getIntValue("id");
        String name = jsonObject.getString("name");
        //logger.info("id = {}, name = {}", id, name);
        //  id = 2, name = fastjson2
        text = "[2, \"fastjson2\"]";
        JSONArray array = JSON.parseArray(text);
        id = array.getIntValue(0);
        name = array.getString(1);
        //logger.info("id2 = {}, name2 = {}", id, name);
        // id2 = 2, name2 = fastjson2

        // 2.5.2 读取JavaBean
        array = new JSONArray();
        array.add(user);
        user = array.getObject(0, User.class);
        //logger.info("user1 - {}", user);
        // user1 - User(id=100, username=jiejie, password=1234)
        jsonObject = new JSONObject();
        jsonObject.put("key", user);
        user = jsonObject.getObject("key", User.class);
        //logger.info("user2 - {}", user);
        // user2 - User(id=100, username=jiejie, password=1234)

        // 2.5.3 转为JavaBean
        array = new JSONArray();
        array.add(user);
        List<User> userList = array.toJavaList(User.class);
        //logger.info("userList - {}", userList.get(0));
        // userList - User(id=100, username=jiejie, password=1234)
        //jsonObject = new JSONObject();
        jsonObject = JSON.parseObject(JSON.toJSONString(user));
        user = jsonObject.toJavaObject(User.class);
        //logger.info("user3 - {}", user);
        // user3 - User(id=100, username=jiejie, password=1234)

        // 2.6 将JavaBean对象序列化为JSON
        text = JSON.toJSONString(user);
        bytes = JSON.toJSONBytes(user);
        //logger.info("text - {}", text);
        //  text - {"id":100,"password":"1234","username":"jiejie"}
        //logger.info("bytes - {}", bytes);
        // bytes - [123, 34, 105, 100, 34, 58, 49, 48, 48, 44, 34, 112, 97, 115, 115, 119, 111, 114, 100, 34, 58, 34, 49, 50, 51, 52, 34, 44, 34, 117, 115, 101, 114, 110, 97, 109, 101, 34, 58, 34, 106, 105, 101, 106, 105, 101, 34, 125]


        // 3. 进阶使用
        // 3.1 使用JSONB
        // 3.1.1 将 JavaBean 对象序列化 JSONB
        //bytes = JSONB.toBytes(user);
        //logger.info("bytes - {}", bytes);
        // bytes - [-90, 75, 105, 100, -48, 100, 81, 112, 97, 115, 115, 119, 111, 114, 100, 77, 49, 50, 51, 52, 81, 117, 115, 101, 114, 110, 97, 109, 101, 79, 106, 105, 101, 106, 105, 101, -91]
        //bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);
        //logger.info("bytes - {}", bytes);
        // bytes - [-105, -48, 100, 77, 49, 50, 51, 52, 79, 106, 105, 101, 106, 105, 101]

        // 3.1.2 将JSONB数据解析为JavaBean
        bytes = new byte[]{-90, 75, 105, 100, -48, 100, 81, 112, 97, 115, 115, 119, 111, 114, 100, 77, 49, 50, 51, 52, 81, 117, 115, 101, 114, 110, 97, 109, 101, 79, 106, 105, 101, 106, 105, 101, -91};
        //user = JSONB.parseObject(bytes, User.class);
        //logger.info("user1 - {}", user);
        // user1 - User(id=100, username=jiejie, password=1234)
        //bytes = new byte[]{-105, -48, 100, 77, 49, 50, 51, 52, 79, 106, 105, 101, 106, 105, 101};
        //user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);
        //logger.info("user2 - {}", user);

        // 3.2 使用 JSONPath
        // 3.2.1 使用 JSONPath 读取部分数据
        text = JSON.toJSONString(user);
        //logger.info("text - {}", text);
        // text - {"id":100,"password":"1234","username":"jiejie"}
        //JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

        //JSONReader parser = JSONReader.of(text);
        //Object result = path.extract(parser);
        //logger.info("result - {}", result); // result - 100

        // 3.2.2 使用JSONPath读取部分byte[]的数据
        /*bytes = new byte[]{-105, -48, 100, 77, 49, 50, 51, 52, 79, 106, 105, 101, 106, 105, 101};
        path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

        parser = JSONReader.of(bytes);
        result = path.extract(parser);
        logger.info("result - {}", result);*/

        // 3.2.3 使用JSONPath读取部分byte[]的数据
        bytes = new byte[]{-90, 75, 105, 100, -48, 100, 81, 112, 97, 115, 115, 119, 111, 114, 100, 77, 49, 50, 51, 52, 81, 117, 115, 101, 114, 110, 97, 109, 101, 79, 106, 105, 101, 106, 105, 101, -91};
        //path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能

        //parser = JSONReader.ofJSONB(bytes); // 注意这里使用ofJSONB方法
        //result = path.extract(parser);
        //logger.info("result - {}", result); // result - 100

    }

}
